#!/usr/bin/env python
import pkg_resources
import cherrypy
import sys

pkg_resources.require("TurboGears")

from flup.server.scgi       import WSGIServer
from cherrypy._cpwsgi       import wsgiApp
from os.path                import *

# first look on the command line for a desired config file,
# if it's not on the command line, then look for setup.py in 
# this directory. If it's not there, this script is probably 
# installed
if len(sys.argv) > 1:
    cherrypy.config.update(file=sys.argv[1])
elif exists(join(dirname(__file__), "setup.py")):
    cherrypy.config.update(file="dev.cfg")
else:
    cherrypy.config.update(file="prod.cfg")

# import the root controller
from fasttrack.controllers import Root

# instantiate the root controller, and make it use the virtual 
# path filter
cherrypy.root = Root()

# initialize the cherrypy server
cherrypy.server.start(initOnly=True, serverClass=None)

# run the SCGI server with our app
WSGIServer(application=wsgiApp, bindAddress=('127.0.0.1', 4000)).run()
